package com.miplus.ccrm.core.service.conf;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.interfaces.MPJBaseJoin;
import com.github.yulichang.query.MPJQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.core.controller.vo.conf.AlertReqVo;
import com.miplus.ccrm.core.controller.vo.conf.AlertResVo;
import com.miplus.ccrm.core.controller.vo.conf.AnnoReqVo;
import com.miplus.ccrm.core.controller.vo.conf.AnnoResVo;
import com.miplus.ccrm.core.pojo.FiPenaltyHist;
import com.miplus.ccrm.core.pojo.MsgAlerts;
import com.miplus.ccrm.core.mapper.MsgAlertsMapper;
import com.miplus.ccrm.core.pojo.MsgAnnouces;
import com.miplus.ccrm.core.utils.EnumFiChannel;
import com.miplus.ccrm.core.utils.EnumTaskStatus;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author pc
* @description 针对表【msg_alerts】的数据库操作Service实现
* @createDate 2024-04-18 15:56:31
*/
@Service
public class MsgAlertsServiceImpl extends MPJBaseServiceImpl<MsgAlertsMapper, MsgAlerts>
    implements MsgAlertsService{

    @Override
    public IPage<AlertResVo> msgAlertViewList(String $usrid,AlertReqVo paras)
    {
        return selectJoinListPage(new Page<>(paras.getPageNum(), paras.getPageSize()),
                AlertResVo.class,
                new MPJLambdaWrapper<MsgAlerts>()
                        .selectAll(MsgAlerts.class)
                        .eq(MsgAlerts::getUsrid,$usrid)
                        .eq(JPubUtils.IsNotEmpty(paras.getStatus()),MsgAlerts::getStatus,paras.getStatus())
                        .between(JPubUtils.IsNotEmpty(paras.getStartdt()), MsgAlerts::getReminddt,JPubUtils.StringToDate(paras.getStartdt()),JPubUtils.StringToDate(paras.getEnddt()))
                        .orderByDesc("alertid")
        );
    }

    @Override
    public CmdResVo msgAlertReaded(Integer alertid)
    {
        MsgAlerts msgAlerts=new MsgAlerts();
        msgAlerts.setAlertid(alertid);
        msgAlerts.setStatus("1");
        msgAlerts.setReaddt(new Date());

        int ret=baseMapper.updateById(msgAlerts);

        return new CmdResVo(ret,alertid.toString());
    }

    @Override
    public int msgAlertAdd(String title,String msg,String tousr,String crusr)
    {
        MsgAlerts msgAlerts=new MsgAlerts();
        msgAlerts.setStatus("0");
        msgAlerts.setCrdt(new Date());
        msgAlerts.setCrusr(crusr);
        msgAlerts.setAlerttype("info");
        msgAlerts.setContent(msg);
        msgAlerts.setTitle(title);
        msgAlerts.setReminddt(new Date());
        msgAlerts.setUsrid(tousr);

        int ret=baseMapper.insert(msgAlerts);

        return ret;
    }

    @Override
    public CmdResVo getAlertNoReadCount(String $usrid)
    {
        Long num=selectJoinCount(new MPJQueryWrapper<MsgAlerts>()
                .eq("usrid",$usrid)
                .eq("status","0")
        );
        return new CmdResVo(num.intValue());
    }

    @Override
    public int msgAlertAdd(EnumFiChannel fiChannel, String tousr, String crusr, String statusdsc)
    {
        return msgAlertAdd("消息提醒",fiChannel.name+statusdsc,tousr,crusr);
    }
}




